﻿using Advantech.MotionComponent;
using OfficeOpenXml.FormulaParsing.Excel.Functions;
using RRQMSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VisionCore
{
    public class RM_Helper
    {
        private ushort slaveIP = 0;
        bool isRun = true;

        Thread thread_AxisRun;
        private MotionHelper motion;      
        public int Speed { set; get; } = 0;
        

        public RM_Helper(MotionHelper motion, ushort _slaveID)
        {
            this.motion = motion;
            slaveIP = _slaveID;
             thread_AxisRun = new Thread(ThreadFunc_Run);
            thread_AxisRun.IsBackground = true;
            thread_AxisRun.Start();
          
        }

        private void ThreadFunc_Run()
        {
            while (isRun)
            {               
            }
            
        }

        public void GetRM_Ddata()
        {           
             
        }

        private float _speed;
        /// <summary>
        /// 设置定位模式速度
        /// </summary>
        public float Set_Speed
        {
            set
            {
                motion.WriteSDOData(1, slaveIP, (ushort)(0x2017), 00, Speed);
            }
            get
            {
                motion.ReadSDOData(1, slaveIP, (ushort)(0x200C), 00, ref _speed);    
                return _speed;
            }
        }

        /// <summary>
        /// 设置定位模式加速度
        /// </summary>
        public float Set_Acc { get; set; }

        private float torque;
        /// <summary>
        /// 获取定位模式力矩
        /// </summary>
        public float Torque 
        {
            get
            {
                lock (this)
                {
                    motion.ReadSDOData(1, slaveIP, (ushort)(0x2018), 00, ref torque);
                    return torque;
                }
            }
        }


        private byte _start0;

        /// <summary>
        /// 执行定位0-7
        /// 写1触发
        /// </summary>
        public byte Start_0
        {
            set
            {
                lock (this)
                {
                    if (motion.WriteSDOData(1, slaveIP, (ushort)(0x200B), 00, value))
                    {
                        motion.WriteSDOData(1, slaveIP, (ushort)(0x200B), 00, 0);
                    }
                }
            }
            get
            {
                motion.ReadSDOData(1, slaveIP, (ushort)(0x200C), 00, ref _start0);
                return _start0;
            }
        }
        private byte _start1;
        public byte Start_1
        { 
            set
            {
                lock (this)
                {
                    if (motion.WriteSDOData(1, slaveIP, (ushort)(0x200B), 01, value))
                    {
                        motion.WriteSDOData(1, slaveIP, (ushort)(0x200B), 01, 0);
                    }
                }
            }
            get
            {
                motion.ReadSDOData(1, slaveIP, (ushort)(0x200C), 01, ref _start1);
                return _start1;
            }
        }
        private byte _start2;
        public Byte Start_2
        {
            get
            {
                lock (this)
                {
                    motion.ReadSDOData(1, slaveIP, (ushort)(0x200C), 02, ref _start2);
                    return _start2;
                }
            }
        }
        public Byte Start_3 { set; get; }
        public Byte Start_4 { set; get; }
        public Byte Start_5 { set; get; }
        public Byte Start_6 { set; get; }
        public Byte Start_7 { set; get; }

        /// <summary>
        ///命令停止触发
        /// </summary>
        public Byte Command_Stop { set; get; }

        /// <summary>
        /// 初始化
        /// </summary>
        public Byte Go_home
        {
            set
            {
                motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 18, 1);
            }
        }

        /// <summary>
        /// 重置错误
        /// </summary>
        public byte  Reset_err
        {
            set
            {
                if (motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 06, value))
                {
                    motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 06, 0);
                }
            }
        }

        /// <summary>
        /// 伺服系统开始和关闭
        /// 1开启 0关闭
        /// </summary>
        public void SRV_ON(bool enable)
        {
            if (enable)
            {
                motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 07, (Byte)1);
            }
            else
            {
                motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 07, 0);
            }

        }

        /// <summary>
        ///重置力
        /// </summary>
        public Byte Reset_force
        {
            set
            {
                if (motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 15, value))
                {
                    motion.WriteSDOData(1, slaveIP, (ushort)(0x200A), 15, 0);
                }
            }
        }
    }
}
